<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="author" content="逯灿灿">
    <title>模拟选择</title>
    <style>
    *{
        box-sizing: border-box;
    }
    .box{
        width: 800px;
        height: 800px;
        margin: 0 auto;
        border: 1px solid black;
    }
    .choice{
        width: 350px;
        height: 50px;
        margin: 50px auto;
        display: flex;
        text-align: center;
        position: relative;
        cursor: pointer;
    }
    .choice-one{
        width: 175px;
        height: 50px;
        background-color: goldenrod;
        line-height: 50px;
    }
    .choice-two{
        width: 175px;
        height: 50px;
        background-color: gray;
        line-height: 50px;
    }
    .choice-three{
        width: 175px;
        background-color: white;
        border: 1px solid black;
        position: absolute;
        margin: 0;
        padding: 0;
        list-style: none;
        right: 0;
        top: 50px;
        display: none;
    }
    .choice-three li{
        width: 173px;
        height: 50px;
        line-height: 50px;
        border-bottom: 1px dotted rgba(100, 100, 100, 0.5); 
    }
    </style>
</head>

<body>
    <div class="box">
        <div class="choice">
            <div class="choice-one">选择项目：</div>
            <div class="choice-two">未选择</div>
            <ul class="choice-three">
                <li>html</li>
                <li>jQuery</li>
                <li>javascript</li>
                <li>css</li>
            </ul>
        </div>
    </div>
    <script src="./js/jquery.js"></script>
</body>

</html>
<script>
    // var choice = $('.choice-two');
    // var choiceShow = $('.choice-three');
    // var choiceList = $('.choice-three').children('li');
    // choice.click(function(ev){
    //     choiceShow.css('display','block');
    //     ev.stopPropagation();
    // });
    // choiceList.hover(function(){
    //     $(this).css('background-color','gray');
    //     $(this).click(function(){
    //         choice.html($(this).html());
    //         choiceShow.css('display','none');
    //     });
    // },function(){
    //     $(this).css('background-color','white');
    // });
    // $(document).click(function(){
    //     choiceShow.css('display','none');
    // });

    // var isShow = false;
    $('.choice-two').click(function(ev){
        // if(isShow == false){
        //     $('.choice-three').show();
        //     isShow = true;
        // }else{
        //     $('.choice-three').hide();
        //     isShow = false;
        // }
        
        // 如果显示就隐藏反之亦然
        $('.choice-three').toggle();
        ev.stopPropagation();
    });
    $('li').mouseover(function(){
        $(this).css('background-color','gray').siblings().css('background-color','white');
        $(this).click(function(){

            var r1 = $('.choice-two').text();
            if($(this).text() != r1){
                $('.choice-two').html($(this).html());
            }
            // $('.choice-three').hide();可以不写，利用冒泡
            // isShow = false;
        });
    });
    $(document).click(function(){
        $('.choice-three').hide();
        // isShow = false;
    })
    
</script>